<?php
namespace app\know\validate;

use app\know\model\QuestionOptionModel;
use think\Validate;
use think\facade\Log;

class QuestionOptionValidate extends Validate
{
    protected $rule = [
        'content' => 'require|min:1|checkLabel',
        'label' => 'require|min:1|checkLabel',
        'id' => 'require|gt:0',
    ];

    protected $message = [
        'content.require' => '选项内容为必填项',
        'content.min' => '选项内容不能为空',
        'label.require' => '选项名称为必填项',
        'label.min' => '选项名称不能为空',
        'id.require' => 'id不能为空',
    ];

    protected $scene = [
        'add'  => ['content', 'label'],
        'edit' => ['id', 'content'],
        'delete' => ['id'],
    ];

    protected function checkLabel($value, $rule, $data)
    {
        if ($this->currentScene != 'edit') 
        {
            return true;
        }

        $model = new QuestionOptionModel();
        $item  = $model->where('id', '<>', $data['id'])
                        ->where('question_uuid', $data['question_uuid'])
                        ->where('label', $data['label'])
                        ->count();

        if ($item <= 1) 
        {
            return true;
        } 
        else 
        {
            return $data['label'] . "---已经存在---";
        }
    }
}